package com.qian2.pricesharingapplication;

import java.util.Locale;

import com.example.pricesharingapplication.R;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.view.MenuItem;

public class Language {
	private static SharedPreferences sharedPref;
	private static Locale myLocale;
	private Context context;
	private String langPref = "Language";

	public Language(SharedPreferences sharedPref, Context context) {
		this.sharedPref = sharedPref;
		this.context = context;
	}

	public void changeLang(String lang) {
		if (lang.equalsIgnoreCase("")) {
			lang = "zh";
		}
		myLocale = new Locale(lang);
		Locale.setDefault(myLocale);
		Configuration config = new Configuration();
		config.locale = myLocale;
		context.getResources().updateConfiguration(config, null);System.out.println("Language.changeLang(): Change language to " + lang);
		saveLocale(lang);
		return;
	}

	public void loadLocale() {

		String language = sharedPref.getString(langPref, "");

		changeLang(language);

	}

	public void saveLocale(String lang) {
		SharedPreferences.Editor editor = sharedPref.edit();
		editor.putString(langPref, lang);
		editor.commit();
	}

	public String getLanguage() {
		return sharedPref.getString(langPref, "");
		
	}

}
